<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <!-- 表单组件 el-form，通过 label-width 设置 label 的宽度  -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" style="width:80%" placeholder="请选择">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog 有专门放置底部操作栏的插槽，具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为 24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getDepartments, getManagerList, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      let result = await getDepartments()
      if (this.formData.id) {
        result = result.filter(item => item.id !== this.formData.id)
      }
      const isRepeat = result.some(item => item.name === value)
      isRepeat ? callback(new Error(`当前已经有${value}的部门了`)) : callback()
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      let result = await getDepartments()
      if (this.formData.id) {
        result = result.filter(item => item.id !== this.formData.id)
      }
      const isRepeat = result.some(item => item.code === value)
      isRepeat ? callback(new Error(`当前已经有${value}编码了`)) : callback()
    }
    return {
      // 定义表单数据
      formData: {
        name: '', // 部门名称，必填 1-50 个字符，同级部门中禁止出现重复部门
        code: '', // 部门编码，必填 1-50 个字符，部门编码在整个模块中都不允许重复
        managerId: '', // 部门管理者，必填
        introduce: '', // 部门介绍，必填 1-300 个字符
        pid: ''
      },
      // 定义校验规则
      rules: {
        name: [{
          required: true,
          message: '部门名称不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 50,
          message: '部门名称要求1-50个字符',
          trigger: 'blur'
        }, {
          trigger: 'blur',
          validator: checkNameRepeat
        }
        ],
        code: [{
          required: true,
          message: '部门编码不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 50,
          message: '部门编码要求1-50个字符',
          trigger: 'blur'
        }, {
          trigger: 'blur',
          validator: checkCodeRepeat
        }
        ],
        managerId: [{
          required: true,
          message: '部门负责人不能为空',
          trigger: 'change'
        }],
        introduce: [{
          required: true,
          message: '部门介绍不能为空',
          trigger: 'blur'
        },
        {
          trigger: 'blur',
          min: 1,
          max: 300,
          message: '部门介绍要求1-50个字符'
        }
        ]
      },
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    btnOK() {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          if (this.formData.id) {
            await updateDepartments(this.formData)
          } else {
            // 表示可以提交了
            await addDepartments({
              ...this.formData,
              pid: this.currentNodeId
            })
          }
          this.$emit('updateDepartment')
          this.$message.success(`${this.formData.id ? '编辑' : '新增'}部门成功`)
          this.btnCancel()
        }
      })
    },
    async getDepartDetail() {
      this.formData = await getDepartDetail(this.currentNodeId)
    }
  }
}
</script>
